import { BlobReader, BlobWriter, getMimeType, ZipReader, type FileEntry } from "@zip.js/zip.js"

export default async (data: File, password?: string) => {
  // console.log("🚀 ~ password:", password)

  const zipFileReader = new BlobReader(data)
  const zipReader = new ZipReader(zipFileReader)
  const entries = await zipReader.getEntries() as FileEntry[]
  const promises = entries.map(async (entry) => {
    const blob = await entry.getData(new BlobWriter(getMimeType(entry.filename)), { password: password })

    return new Promise((resolve) => {
      resolve(blob ? URL.createObjectURL(blob) : '')
    }) as Promise<string>
  })

  return promises;
}
